package com.ea.admin.controller.company;

import com.ea.admin.permission.PermissionCode;
import com.ea.admin.pojo.Enum.Permission;
import com.ea.admin.pojo.param.department.*;
import com.ea.admin.pojo.vo.account.ContactAdminVo;
import com.ea.admin.pojo.vo.department.DepartmentVo;
import com.ea.admin.service.DepartmentService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.log.LogAnnotation;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "单位管理-部门管理")
@RequestMapping("/admin/department")
public class DepartmentController {
    @Resource
    DepartmentService departmentService;
    @Resource
    NowUserGetter nowUserGetter;

    @Transactional
    @Operation(summary = "获得该用户管理的、某单位下的所有部门")
    @PostMapping("/getDepartmentList")
    @ApiImplicitParam(dataType = "GetDepartmentListParam", name = "getDepartmentListParam", value = "获得部门信息", readOnly = true, required = true)
    public Result<List<DepartmentVo>> getDepartmentList(@RequestBody @Valid GetDepartmentListParam getDepartmentListParam) {
        List<DepartmentVo> result = departmentService.getDepartmentList(
                ParamUtil.parseIdList(getDepartmentListParam.getCompanyIdList()),
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "联系人搜索")
    @PostMapping("/searchContact")
    @ApiImplicitParam(dataType = "SearchDepartmentContactParam", name = "searchDepartmentContactParam", value = "联系人搜索", readOnly = true, required = true)
    @LogAnnotation(module = "CompanyController", operation = "联系人搜索")
    @Permission(permission_code = PermissionCode.ADD_DEPT)
    public Result<List<ContactAdminVo>> searchContact(@RequestBody @Valid SearchDepartmentContactParam searchDepartmentContactParam) {
        List<ContactAdminVo> result = departmentService.searchContact(
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(searchDepartmentContactParam.getCompanyId()),
                searchDepartmentContactParam.getContactName()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "添加新的部门")
    @PostMapping("/addDepartment")
    @ApiImplicitParam(dataType = "AddDepartmentParam", name = "addDepartmentParam", value = "添加新的部门", readOnly = true, required = true)
    @LogAnnotation(module = "DepartmentController", operation = "添加新部门")
    @Permission(permission_code = PermissionCode.ADD_DEPT,
            content = "在单位“{companyName}”下添加部门“{deptName}”，设置默认管理员为“{deptContactUsername}”，姓名为“{deptContactName}”，手机号码为“{deptContactPhoneNum}”")
    public Result<String> addDepartment(@RequestBody @Valid AddDepartmentParam addDepartmentParam) {
        Long data=departmentService.addDepartment(
                addDepartmentParam.getDeptName(),
                ParamUtil.parseId(addDepartmentParam.getSupDeptId()),
                ParamUtil.parseId(addDepartmentParam.getCompanyId()),
                nowUserGetter.getNowUser().getAccountId(),
                ParamUtil.parseId(addDepartmentParam.getDeptContactAccountId()),
                addDepartmentParam.getDeptContactName(),
                addDepartmentParam.getDeptContactPhoneNum(),
                addDepartmentParam.getDeptContactUsername(),
                addDepartmentParam.getDeptContactPassword(),
                addDepartmentParam.getDeptContactRePassword()

        );
        return Result.success(String.valueOf(data));
    }

    @Transactional
    @Operation(summary = "修改部门信息")
    @PostMapping("/editDepartment")
    @ApiImplicitParam(dataType = "EditDepartmentParam", name = "editDepartmentParam", value = "修改部门信息", readOnly = true, required = true)
    @LogAnnotation(module = "DepartmentController", operation = "修改部门")
    @Permission(permission_code = PermissionCode.EDIT_DEPT,
            content = "修改部门“{deptName}”信息")
    public Result editDepartment(@RequestBody @Valid EditDepartmentParam editDepartmentParam) {
        departmentService.editDepartment(
                ParamUtil.parseId(editDepartmentParam.getDeptId()),
                editDepartmentParam.getDeptName(),
                ParamUtil.parseId(editDepartmentParam.getSupDeptId()),
                ParamUtil.parseId(editDepartmentParam.getDeptContactId()),
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "删除部门")
    @PostMapping("/deleteDepartment")
    @ApiImplicitParam(dataType = "DeleteDepartmentParam", name = "deleteDepartmentParam", value = "删除部门", readOnly = true, required = true)
    @LogAnnotation(module = "DepartmentController", operation = "删除部门")
    @Permission(permission_code = PermissionCode.DELETE_DEPT,
            content = "删除部门“{deptName}”")
    public Result deleteDepartment(@RequestBody @Valid DeleteDepartmentParam deleteDepartmentParam) {
        departmentService.deleteDepartment(
                ParamUtil.parseId(deleteDepartmentParam.getDeptId()),
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(null);
    }
}
